from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as plt

# 这两句解决OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.报错问题
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

x_data = np.random.rand(100)
noise = np.random.normal(0, 0.01, x_data.shape)
y_data = 0.1 * x_data + 0.2 + noise

plt.scatter(x_data, y_data)
plt.show()

model = Sequential()
model.add(Dense(1, input_shape=(1,)))
model.compile(optimizer=SGD(), loss="MSE")

for i in range(6000):
    cost = model.train_on_batch(x_data, y_data)
    if i % 500 == 0:
        print('step:', i, '    cost:', cost)

W, b = model.layers[0].get_weights()
print(W, b)

y_pred = model.predict(x_data)
plt.scatter(x_data, y_data)
plt.plot(x_data, y_pred, 'r-', lw=3)
plt.show()

x_data = np.linspace(-1, 1, 200)
noise = np.random.normal(0, 0.1, x_data.shape)
y_data = np.square(x_data) + noise

plt.scatter(x_data, y_data)
plt.show()

model = Sequential()
model.add(Dense(5, input_shape=(1,), activation='tanh'))
model.add(Dense(1))
model.compile(optimizer=SGD(), loss="MSE")

for i in range(6000):
    cost = model.train_on_batch(x_data, y_data)
    if i % 500 == 0:
        print('step:', i, '    cost:', cost)

W, b = model.layers[0].get_weights()
print(W, b)

y_pred = model.predict(x_data)
plt.scatter(x_data, y_data)
plt.plot(x_data, y_pred, 'r-', lw=3)
plt.show()
